package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ImgDaoImpl;
import com.ysd.entity.Img;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminImgServlet extends BaseServlet {

	/**
	 * 分页条
	 * localhost:8080/kouhong/adminImg?reqName=getImg
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getImg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ImgDaoImpl iDao = new ImgDaoImpl();
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<Img> iList = iDao.getImgPage(page, size);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		String res = gson.toJson(iList);
		out.print(res);
		out.flush();
		out.close();
	}
	/**
	 * 删除查询图片
	 * localhost:8080/kouhong/adminImg?reqName=getImgPage
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getImgPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ImgDaoImpl iDao = new ImgDaoImpl();
	
		List<Img> iList = iDao.getImg();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		String res = gson.toJson(iList.size());
		out.print(res);
		out.flush();
		out.close();
	}
	/**
	 * 添加图片
	 * localhost:8080/kouhong/adminImg?reqName=addImg
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addImg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		ImgDaoImpl iDao = new ImgDaoImpl();
		String imgPath = request.getParameter("imgPath");
		Img i = new Img(0, imgPath);
		int n = iDao.addImgs(i);

		if (n>0) {
			out.print(Result.toClient(true, "添加成功", ""));
		} else {
			out.print(Result.toClient(false, "添加失败", ""));
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * 通过id查询
	 * localhost:8080/kouhong/adminImg?reqName=getImgById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getImgById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		ImgDaoImpl iDao = new ImgDaoImpl();
		int id = 0;
		if(request.getParameter("imgId")!=null && !request.getParameter("imgId").equals("")) {
			 id = Integer.parseInt(request.getParameter("imgId"));
		}
		
		Img i = iDao.getImgById(id);
		
		Gson gson = new Gson();
		out.print(gson.toJson(i));
		out.flush();
		out.close();
	}
	
	/**
	 * 修改
	 * localhost:8080/kouhong/adminImg?reqName=updateImg
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateImg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		ImgDaoImpl iDao = new ImgDaoImpl();
		int imgId = 0;
		if(request.getParameter("imgId")!=null && !request.getParameter("imgId").equals("")) {
			 imgId=Integer.parseInt(request.getParameter("imgId"));
		}
		String imgPath = request.getParameter("imgPath");
		Img i = new Img(imgId, imgPath);
		int n = iDao.updateImgs(i);
		
		if (n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		
		out.flush();
		out.close();
	}
	
	
	/**
	 * 批量删除
	 * localhost:8080/kouhong/adminImg?reqName=deleteImgById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteImgById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		ImgDaoImpl iDao = new ImgDaoImpl();
		String imId = request.getParameter("imgId");
		int n = iDao.deleteImgById(imId);
		
		if (n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * 添加图片
	 * localhost:8080/kouhong/adminImg?reqName=delImg
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delImg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		ImgDaoImpl iDao = new ImgDaoImpl();
		int imId =0;
		if(request.getParameter("imgId")!=null && !request.getParameter("imgId").equals("")) {
			imId = Integer.parseInt(request.getParameter("imgId"));
		}
		
		int n = iDao.delImgById(imId);
		if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
	}
	
}
